package com.batchOperation.excel.executor;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.batchOperation.Template.model.BatchOperationTemplate;
import com.batchOperation.Template.model.BatchOperationTemplateContent;
import com.batchOperation.excel.listener.ObjectListener;
import com.batchOperation.excel.model.Employee;
import com.batchOperation.excel.model.SchoolInfo;
import com.batchOperation.excel.support.ExcelConstant;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Package: com.batchOperation.excel.executor.serviceImpl
 * @ClassName: AbstractExecutor
 * @Author: h2cloud
 * @CreateTime: 2024/7/19 11:34 下午
 * @Description:
 */
@Slf4j
public class AbstractExecutor {

	/**
	 * 获取操作Id
	 *
	 * @return
	 */
	public Long getExecutorId() {
		return System.currentTimeMillis();
	}

	@SneakyThrows
	public <T> List<List<String>> getHeadList(Class<T> clz, BatchOperationTemplate batchOperationTemplate) {
		List<String> fieldNameList = batchOperationTemplate.getContents().stream().filter(content -> content.getEntity().equals(clz.getSimpleName())).map(BatchOperationTemplateContent::getFieldId).collect(Collectors.toList());
		List<List<String>> headList = new ArrayList<>();
		for (String fieldName : fieldNameList) {
			Field field = clz.getDeclaredField(fieldName);
			String[] headNames = field.getAnnotation(ExcelProperty.class).value();
			headList.add(Arrays.asList(headNames));
		}
		return headList;
	}

	public <T> List<T> getSheetData(Class<T> clz, Long executorId, int sheetNo, String fileName, BatchOperationTemplate templateInfo) {
		ObjectListener<T> objectListener = new ObjectListener(executorId);
		EasyExcel.read(ExcelConstant.path + fileName, clz, objectListener)
				.sheet(sheetNo)
				.headRowNumber(getHeadNumber(clz, templateInfo))
				.doRead();
		return objectListener.getList();
	}

	@SneakyThrows
	public <T> int getHeadNumber(Class<T> clz, BatchOperationTemplate batchOperationTemplate) {
		List<String> fieldNameList = batchOperationTemplate.getContents().stream()
				.filter(content -> content.getEntity().equals(clz.getSimpleName()))
				.map(BatchOperationTemplateContent::getFieldId)
				.collect(Collectors.toList());
		int headNumber = 0;
		for (String fieldName : fieldNameList) {
			Field field = clz.getDeclaredField(fieldName);
			String[] headNames = field.getAnnotation(ExcelProperty.class).value();
			headNumber = Math.max(headNumber, headNames.length);
		}
		return headNumber;
	}

	public FileInputStream getInputStream(String fileName) {
		FileInputStream inputStream = null;
		try {
			inputStream = new FileInputStream(ExcelConstant.path + fileName);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return inputStream;
	}
}
